//
//  KDReportInfoController.m
//  WangJingApp
//
//  Created by 魏强 on 17/11/13.
//  Copyright © 2017年 KDTech. All rights reserved.
//

#import "KDReportInfoController.h"
#import "KDReportRequestTool.h"
#import "KDTabBarController.h"
#import "NSDictionary+WQExtension.h"
#import "KDCompany.h"
#import "KDClueType.h"
#import "KDCity.h"
#import "KDInfoType.h"
#import "NSString+KD.h"
#import "KDSingleWebController.h"
@interface KDReportInfoController ()
/**  scroll的宽度约束 */
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *scroll_cons_w;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *secondView_cons_w;

@property (weak, nonatomic) IBOutlet UILabel *reportTitleLb;
@property (weak, nonatomic) IBOutlet UILabel *reportTypeLb;
@property (weak, nonatomic) IBOutlet UILabel *infoTypeLb;
@property (weak, nonatomic) IBOutlet UILabel *reportContentLb;
/**  核查人 */
@property (weak, nonatomic) IBOutlet UILabel *checkManLb;
/**  审核人 */
@property (weak, nonatomic) IBOutlet UILabel *AuditorLb;
/**  物理地域 */
@property (weak, nonatomic) IBOutlet UILabel *physicRegionLb;
/**  活动形式 */
@property (weak, nonatomic) IBOutlet UILabel *activityLb;
@property (weak, nonatomic) IBOutlet UILabel *infoLevelLb;
/**  信息归属地 */
@property (weak, nonatomic) IBOutlet UILabel *attributionLb;
@property (weak, nonatomic) IBOutlet UILabel *keywordLb;
/**  次关键词 */
@property (weak, nonatomic) IBOutlet UILabel *secondaryKwLb;
/**  工作要求 */
@property (weak, nonatomic) IBOutlet UILabel *workRequireLb;

@property (weak, nonatomic) IBOutlet UILabel *otherDescribLb;

@property (weak, nonatomic) IBOutlet UILabel *clueTypeLb;
/**  涉及辖区 */
@property (weak, nonatomic) IBOutlet UILabel *areaLb;
@property (weak, nonatomic) IBOutlet UILabel *infoTitleLb;
@property (weak, nonatomic) IBOutlet UILabel *infoTimeLb;
@property (weak, nonatomic) IBOutlet UILabel *authorLb;
@property (weak, nonatomic) IBOutlet UILabel *webNameLb;
@property (weak, nonatomic) IBOutlet UILabel *webTypeLb;
@property (weak, nonatomic) IBOutlet UIButton *urlBtn;
@property (weak, nonatomic) IBOutlet UILabel *infoContentLb;


/**  所有的网站类型 */
@property (nonatomic, strong) NSArray *webTypes;

/**
 *  线索模型的汇总
 */
@property (nonatomic, strong) NSArray *clues;


/**  所有可上报的接收单位 */
@property (nonatomic, strong) NSArray *Companys;

/**  所有的线索来源类型 */
@property (nonatomic, strong) NSArray *cueSources;

/**  所有可供选择的辖区 */
@property (nonatomic, strong) NSArray *citys;

/**  所有可供选择的信息类别 */
@property (nonatomic, strong) NSArray *infoTypes;
/**  当计数为2时，可以开始布置下拉菜单 */
@property (nonatomic, assign) NSInteger addUIClock;
@end

@implementation KDReportInfoController
#pragma mark - 懒加载区
- (void)setModel:(KDReportListCellModel *)model{
    _model = model;
}
#pragma mark - 生命周期区
- (void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
//    static dispatch_once_t onceToken;
//    dispatch_once(&onceToken, ^{
         [self leloadData];
//    });
   
   
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = BackGroundColor;
     [self loadData];

}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.scroll_cons_w.constant = screenW - 20;
    self.secondView_cons_w.constant = screenW - 20;
}

- (void)leloadData{
    self.reportTitleLb.text = _model.report_title;
    switch (_model.report_type) {
        case 1:
            self.reportTypeLb.text = @"单条";
            break;
        case 2:
            self.reportTypeLb.text = @"综述";
            break;
        case 3:
            self.reportTypeLb.text = @"落深度研判";
            break;
        default:
            break;
    }
    self.reportContentLb.attributedText = [NSString praseHtmlStr:_model.report_content];
    self.checkManLb.text = _model.editor;
    self.AuditorLb.text = _model.send_comp_name;
    //    self.clueTypeLb.text = model.source_type;
    //    self.physicRegionLb = model.regions
    self.infoTitleLb.text = _model.info_title;
    self.infoTimeLb.text = _model.report_time;

    self.webNameLb.text = _model.website_name;
    //    self.webTypeLb.text = model.website_type;
    [self.urlBtn setTitle:_model.url forState:UIControlStateNormal];
    if (self.model.info_content) {
        self.infoContentLb.attributedText = [NSString praseHtmlStr:_model.info_content];
    }
    switch (_model.w_type) {
        case 1:
            self.infoLevelLb.text = @"一级";
            break;
        case 2:
            self.infoLevelLb.text = @"二级";
            break;
        case 3:
            self.infoLevelLb.text = @"三级";
            break;
        case 4:
            self.infoLevelLb.text = @"四级";
            break;
        case 5:
            self.infoLevelLb.text = @"五级";
            break;
            
        default:
            break;
    }
}
#pragma mark - 板块布置区

#pragma mark - 事件点击区
- (IBAction)urlBtnClick:(UIButton *)sender {
    KDSingleWebController *singleWeb = [[KDSingleWebController alloc]init];
    singleWeb.webUrl = sender.currentTitle;
    [self.navigationController pushViewController:singleWeb animated:YES];
}

#pragma mark - 网络请求区
- (void)loadData{
    //1.获取到网站类型
    WS(ws);
//    __weak __typeof(self)weakSelf = self;
    [KDReportRequestTool getWebsitTypesWithParam:nil success:^(KDBaseResult *responseObejct) {
        if ([responseObejct.code isEqualToString:@"1"]) {
            NSDictionary *dic = [NSDictionary dictionaryWithJsonString:(NSString *)responseObejct.data];
            
            NSArray *arr = [dic allValues];
//            ws.webTypes = arr;
            ws.webTypeLb.text = arr[ws.model.website_type];
        }else if ([responseObejct.code isEqualToString:@"E040110000001"]){
            [self.navigationController dismissViewControllerAnimated:NO completion:^{
                KDNewfeatureController *new = [[KDNewfeatureController alloc]init];
                __block KDTabBarController*ctr = (KDTabBarController *)[UIApplication sharedApplication].keyWindow.rootViewController;
                [UIApplication sharedApplication].keyWindow.rootViewController = new;
                [ctr removeObserver];
                [ctr removeFromParentViewController];
                ctr = nil;
            }];
        }else{
            [SVProgressHUD showErrorWithStatus:responseObejct.msg maskType:SVProgressHUDMaskTypeGradient];
        }
    } failure:^(NSError *error) {
        KDLog(@"%@", error);
        //        [SVProgressHUD showErrorWithStatus:@"" maskType:<#(SVProgressHUDMaskType)#>]
    }];
    
    
    //2.获取到可接收报告的单位
//    [KDReportRequestTool getCompanysWithParam:nil success:^(KDBaseResult *responseObejct) {
//        if ([responseObejct.code isEqualToString:@"1"]) {
//            self.Companys = [KDCompany objectArrayWithKeyValuesArray:responseObejct.info];
//            weakSelf.addUIClock++;
//        }
//    } failure:^(NSError *error) {
//        KDLog(@"%@", error);
//    }];
    
    //3.获取所有线索
    [KDReportRequestTool getCueSourcesWithParam:nil success:^(KDBaseResult *responseObejct) {
        if ([responseObejct.code isEqualToString:@"1"]) {
            NSArray *cues = [KDClueType objectArrayWithKeyValuesArray:responseObejct.data];
            for (KDClueType *cue in cues ) {
                if (cue.clueType_id == ws.model.source_type) {
                    ws.clueTypeLb.text = cue.cue_name;
                    break;
                }
            }
//            weakSelf.addUIClock++;
        }else{
            [SVProgressHUD showErrorWithStatus:responseObejct.msg maskType:SVProgressHUDMaskTypeGradient];
        }
    } failure:^(NSError *error) {
        KDLog(@"%@", error);
    }];
    
    
    //4.获取所有城市
    NSArray *arr = [self.model.regions JSONValue];
    KDLog(@"%@",arr);
    
    if (arr.count) {//有相关城市才请求
        [KDReportRequestTool getCitysWithParam:nil success:^(KDBaseResult *responseObejct) {
            if ([responseObejct.code isEqualToString:@"1"]) {
                NSArray *citys = [KDCity objectArrayWithKeyValuesArray:responseObejct.info];
                NSMutableString *regions = [NSMutableString string];
                
                for (NSNumber *str in arr) {
                    for (KDCity *city in citys) {
                        if (![str isKindOfClass:[NSNumber class]]) {
                            continue;
                        }
                        if (city.city_id == str.integerValue) {
                            [regions appendString:city.city_name];
                            [regions appendString: @" "];
                            break;
                        }
                    }
                }
                if (regions.length) {
                    ws.physicRegionLb.text = regions;
                    ws.areaLb.text = regions;
                }
            }else{
                [SVProgressHUD showErrorWithStatus:responseObejct.msg maskType:SVProgressHUDMaskTypeGradient];
            }
        } failure:^(NSError *error) {
            KDLog(@"%@", error);
        }];
    }
   
    
    //5.获取信息类别
    [KDReportRequestTool getInfoTypesWithParam:nil success:^(KDBaseResult *responseObejct) {
        if ([responseObejct.code isEqualToString:@"1"]) {
            NSArray * infoTypes = [KDInfoType objectArrayWithKeyValuesArray:responseObejct.info];
            for (KDInfoType *infoType in infoTypes) {
                if (infoType.ID == ws.model.about_type) {
                    ws.infoTypeLb.text = infoType.info_type_name;
                    break;
                }
            }
        }else{
            [SVProgressHUD showErrorWithStatus:responseObejct.msg maskType:SVProgressHUDMaskTypeGradient];
        }
    } failure:^(NSError *error) {
        KDLog(@"%@", error);
    }];
    
}
#pragma mark - 通知接收区


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
